/*
 * HomeBudgetApp.java
 */
package homebudget;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class HomeBudgetApp extends SingleFrameApplication {

    private static int idbudzetu=1;
    private static String connectionString="localhost:3306/budzet";
    private static String login="root";
    private static String haslo="root";

    public static String getHaslo() {
        return haslo;
    }

    public static void setHaslo(String haslo) {
        HomeBudgetApp.haslo = haslo;

    }

    public static String getLogin() {
        return login;
    }

    public static void setLogin(String login) {
        HomeBudgetApp.login = login;
    }
    public static int getIdbudzetu() {
        return idbudzetu;
    }
    public static void setIdbudzetu(int idbudzetu) {
        HomeBudgetApp.idbudzetu = idbudzetu;
    }

    public static String getConnectionString() {
        return connectionString;
    }
    public static void setConnectionString(String connectionString) {
        HomeBudgetApp.connectionString = connectionString;
    }
    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        DataBaseMySql baza = new DataBaseMySql();
        wczytajZPliku("MySQL.ini");

        if(!baza.stworzBaze())
             JOptionPane.showMessageDialog(this.getMainFrame(), "Błedny login lub haslo do bazy MySQL ","Bład",JOptionPane.ERROR_MESSAGE);
        

        show(new HomeBudgetView(this));
    }
    public void wczytajZPliku(String filename){
        String s = new String();
        StringBuffer sb = new StringBuffer();
        FileReader fr;

        try {
            fr = new FileReader(new File(filename));

            BufferedReader br = new BufferedReader(fr);
            while((s = br.readLine()) != null)
            {
                sb.append(s);
            }
            br.close();
            } catch (Exception ex) {
                Logger.getLogger(HomeBudgetApp.class.getName()).log(Level.SEVERE, null, ex);
            }

            s = sb.toString();
            String login = s.substring(s.indexOf(':')+1,s.indexOf(';'));
            String haslo = s.substring(s.lastIndexOf(':')+1,s.lastIndexOf(';'));

            login = login.replaceAll(" ", "");
            haslo = haslo.replaceAll(" ", "");
            System.out.println("Wczytano: l:"+login+" p:"+haslo);
            setLogin(login);
            setHaslo(haslo);

    }
    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of HomeBudgetApp
     */
    public static HomeBudgetApp getApplication() {
        return Application.getInstance(HomeBudgetApp.class);
    }
    
    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(HomeBudgetApp.class, args);

          


    }
}
